package cn.yyx.staservice.controller;

import cn.yyx.common.R;
import cn.yyx.customerservice.entity.Customer;
import cn.yyx.empservice.entity.employee.Employee;
import cn.yyx.empservice.service.EmployeeService;
import cn.yyx.staservice.entity.TOrder;
import cn.yyx.staservice.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2023-10-25
 */
@RestController
public class TOrderController {
    @Autowired
    private TOrderService tOrderService;
    @Autowired
    private EmployeeService employeeService;


    /**
     * 新增客户统计（按照月份计算）
     * @param type
     * @param startmonth 开始月份
     * @param endmonth 结束月份
     * @return
     */
    @GetMapping("/customer/stamonth/{searchObj.type}/{searchObj.begin}/{searchObj.end}")
    public R getMonthDataSta(@PathVariable(name = "searchObj.type") String type,@PathVariable(name = "searchObj.begin") String startmonth,@PathVariable(name = "searchObj.end") String endmonth){
        System.out.println(startmonth);
        System.out.println(endmonth);
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("date_format(order_date,'%Y-%m') as month,count(*) as count").between("date_format(order_date,'%Y-%m-%d')",startmonth,endmonth)
                .groupBy("date_format(order_date,'%Y-%m')").orderByAsc("month");
        List<Map<String, Object>> list = tOrderService.listMaps(queryWrapper);
        if (list.isEmpty()) {
            return R.ok();
        }
        List numDataList =new ArrayList<>();
        List date_calculatedList = new ArrayList<>();
        list.forEach(map -> {
            date_calculatedList.add(map.get("month"));
            numDataList.add(map.get("count"));
        });
        if (type.equals("by_month")) {
            return R.ok().data("numDataList", numDataList).data("date_calculatedList", date_calculatedList).data("type", "月/(12)");
        }else{
            return R.ok().data("numDataList", numDataList).data("date_calculatedList", date_calculatedList).data("type", "周");
        }
    }

    /**
     * 统计每个月的业绩总额
     * @return
     */
    @GetMapping("/sta")
    public R  getAchievementDataSta(){
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("date_format(order_date,'%Y-%m') as month,sum(order_price) as total")
                .groupBy("date_format(order_date,'%Y-%m')").orderByAsc("month");
        List<Map<String, Object>> list = tOrderService.listMaps(queryWrapper);
        if (list.isEmpty()) {
            return R.ok();
        }
        List numDataList = new ArrayList<>();
        List date_calculatedList = new ArrayList<>();
        list.forEach(map -> {
            numDataList.add(map.get("total"));
            date_calculatedList.add(map.get("month"));
        });
        return R.ok().data("numDataList", numDataList).data("date_calculatedList",date_calculatedList).data("type","月");
    }

    /**
     * 在指定时间内，员工销售额统计
     * @param startmonth
     * @param endmonth
     * @return
     */
    @GetMapping("/sta/percentage/{searchObj.begin}/{searchObj.end}")
    public R getPercentageDataSta(@PathVariable(value = "searchObj.begin") String startmonth,@PathVariable(value = "searchObj.end") String endmonth){
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sale_id,SUM(order_price) AS total")
                .between("date_format(order_date,'%Y-%m-%d')",startmonth,endmonth)
                .groupBy("sale_id");
        List<Map<String, Object>> list = tOrderService.listMaps(queryWrapper);
        if (list.isEmpty()) {
            return R.ok();
        }
        List<String> nameList = new ArrayList<>();
        List saleScoreList = new ArrayList<>();
        list.forEach(map -> {
            Employee saleId = employeeService.getById(map.get("sale_id").toString());
            nameList.add(saleId.getName());
            Map<String ,Object> rmap = new HashMap<>();
            rmap.put("name",saleId.getName());
            rmap.put("value",map.get("total")) ;
            saleScoreList.add(rmap);
        });
        return R.ok().data("nameList",nameList).data("saleScoreList",saleScoreList);
    }
}

